package org.example.kafka.commit;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.example.kafka.common.CommonConstant;
import org.example.kafka.common.KafkaConstants;

import java.util.Collections;
import java.util.Properties;

/**
 * 异步提交偏移量，生产者使用ProducerCommit
 */
public class CommitAsync {
    public static void main(String[] args) {
        /*消息消费者*/
        Properties properties = KafkaConstants.consumerConfig(
                "CommitAsync",
                StringDeserializer.class,
                StringDeserializer.class);
        // 取消自动提交
        properties.put("enable.auto.commit",false);

        KafkaConsumer<String,String> consumer
                = new KafkaConsumer<String, String>(properties);
        try{
            consumer.subscribe(Collections.singletonList(CommonConstant.CONSUMER_COMMIT_TOPIC));
            while (true){
                ConsumerRecords<String,String> records = consumer.poll(500);
                for(ConsumerRecord<String, String> record:records){
                    System.out.println(String.format(
                            "主题：%s，分区：%d，偏移量：%d，key：%s，value：%s",
                            record.topic(),record.partition(),record.offset(),
                            record.key(),record.value()));
                    //do our work
                }
                consumer.commitAsync();

                /*允许执行回调*/
//                consumer.commitAsync(new OffsetCommitCallback() {
//                    public void onComplete(
//                            Map<TopicPartition, OffsetAndMetadata> offsets,
//                            Exception exception) {
//                        if(exception!=null){
//                            System.out.print("Commmit failed for offsets ");
//                            System.out.println(offsets);
//                            exception.printStackTrace();
//                        }
//                    }
//                });
            }
        }finally {
            consumer.close();
        }

    }
}
